package cn.newgrand.netcall.consultant;

import javax.swing.JOptionPane;

import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.SparkManager;

import cn.newgrand.common.StringUtil;
import cn.newgrand.common.URLMappingUtil;
import cn.newgrand.netcall.consultant.ui.SpecialistGroup;
import cn.newgrand.netcall.consultant.ui.SpecialistList;
import cn.newgrand.smackx.consultant.ConsultantManager;
import cn.newgrand.smackx.consultant.packet.SpecialistNotifyPacket;
import cn.newgrand.smackx.consultant.packet.SpecialistPresence;

public class URLConsultantManager {
	
	private static URLConsultantManager instance = null;
	public static final String OPEN_SPECIALISTTREE_HEADER="open_tree/?";
	public static final String OPEN_TREENODE_NAME="open_treenode_name";

	private URLConsultantManager(){
		
	}
	
	public static boolean isToSpecialistTree(String url){
		if(!URLMappingUtil.isStandardProtocol(url)) return false;
		return url.indexOf(OPEN_SPECIALISTTREE_HEADER)!=-1;
	}
	public static String getTreeNodeName(String url){
		if(!isToSpecialistTree(url))
			return null;
		return URLMappingUtil.getParameter(url,OPEN_TREENODE_NAME);
	}
	
	public synchronized static URLConsultantManager getInstance(){
		
		if(instance == null){
			instance = new URLConsultantManager();
		}
		
		return instance;
	}
	
	public static void handleURIMapping(String arguments){
		if(arguments == null || "".equals(arguments)){
			return;
		}
		if(URLMappingUtil.isToConsultant(arguments)){
        	String spid = URLMappingUtil.getConsultantSpid(arguments);
        	if(spid == null || "".equals(spid)) return ;
        	ConsultantManager consultantManager = ConsultantManager.getInstance(SparkManager.getConnection());
        	SpecialistPresence sprPresence = consultantManager.getRoster().getPresence(spid);
        	if(sprPresence.isAvailable()){
        		SpecialistNotifyPacket result = consultantManager.joinSpecialistQueue(spid);
        		if(result.getStatus().equals("yes")){
        			SpecialistQueueManager.getInstance().putWhenJoin(spid);
        			JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "加入成功");
        		}
        		else{
        			if(result.getResult().equals("busy"))
        				JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "专家正忙，队列已满");
        			else if(result.getResult().equals("repeat"))
        				JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "重复加入队列");
        			else if(result.getResult().equals("sameone"))
        				JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "不能加入自己队列");
        			else
        				JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "无法加入队列");
        		}
        	}else{
        		JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "专家不在线");
        	}
        }else if(isToSpecialistTree(arguments)){
        	String treeNodeName = getTreeNodeName(arguments);
        	SpecialistList sp  = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSpecialistList();
        	sp.getTabbedPane().setSelectedIndex(sp.getTabbedPane().indexOfTab("<html>行<br>业</html>"));
        	for(SpecialistGroup s:sp.getContactGroups()){
        		if(s.getGroupName().equals("行业::"+treeNodeName)){
        			s.setCollapsed(false);
        		}else{
        			s.setCollapsed(true);
        		}
        	}
        }
	}
}
